package edu.ccut.saturn.studio.dialog.transformdialog;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnContent;

public class MyCellModifierSaturnContent implements ICellModifier {

	private TableViewer tableViewer;
	
	public MyCellModifierSaturnContent(TableViewer tableViewer){
		this.tableViewer = tableViewer;
	}
	public boolean canModify(Object element, String property) {
		return true;
	}

	public Object getValue(Object element, String property) {
		SaturnContent o = (SaturnContent) element;
		if (property.equals("id")) {
			return o.getId();
		} else if (property.equals("type")) {
			return getTypeIndex(o.getType());
		} else if (property.equals("contentValueKey")){
			return o.getContentValueKey();
		}
			throw new RuntimeException("" + property); 
	}

	private int getTypeIndex(String classType) {
		for (int i = 0; i < SelectionCandidate.transformClassTypes.length; i++) {
			if (SelectionCandidate.transformClassTypes[i].equals(classType))
				return i;
		}
		return -1;
	}
	
	public void modify(Object element, String property, Object value) {
		TableItem item = (TableItem) element;
		SaturnContent o = (SaturnContent) item.getData();
		if (property.equals("type")) {
			Integer comboIndex = (Integer) value;
			String newType = SelectionCandidate.transformClassTypes[comboIndex];
			o.setType(newType);
		} else if (property.equals("contentValueKey")) {
			String newContentValueKey = (String) value;
			o.setContentValueKey(newContentValueKey);
		}  else {
			throw new RuntimeException("" + property); 
		}
		tableViewer.update(o, null);
	}
}
